package com.randi.common.core.domain.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.randi.common.core.domain.BaseEntity;
import com.randi.common.enums.MenuTypeEnum;
import com.randi.common.utils.uuid.IdUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.annotation.Transient;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ziye
 */
@EqualsAndHashCode(callSuper = true)
@Data
@TableName("sys_menu")
@ApiModel(value = "SysMenu对象", description = "SysMenu对象")
public class SysMenu extends BaseEntity {

    @TableId(value = "menu_id", type = IdType.AUTO)
    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "菜单ID")
    private Long menuId;

    @NotBlank(message = "菜单名称不能为空")
    @TableField(value = "`menu_name`")
    @ApiModelProperty(value = "菜单名称")
    private String menuName;

    @TableField(value = "`parent_id`")
    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "父菜单ID")
    private Long parentId;

    @TableField(value = "`menu_type`")
    @ApiModelProperty(value = "菜单类型")
    private MenuTypeEnum menuType = MenuTypeEnum.CATALOG;

    @TableField(value = "`menu_icon`")
    @ApiModelProperty(value = "菜单图标")
    private String menuIcon;

    @NotBlank(message = "权限标识不能为空")
    @TableField(value = "`menu_code`")
    @ApiModelProperty(value = "权限标识")
    private String menuCode;

    @NotBlank(message = "路由地址不能为空")
    @TableField(value = "`menu_path`")
    @ApiModelProperty(value = "路由地址")
    private String menuPath;

    @TableField(value = "`redirect`")
    @ApiModelProperty(value = "重定向地址")
    private String redirect;

    @NotBlank(message = "组件路径不能为空")
    @TableField(value = "`component`")
    @ApiModelProperty(value = "组件路径")
    private String component;

    @TableField(value = "`query`")
    @ApiModelProperty(value = "路由参数")
    private String query;

    @TableField(value = "`order_num`")
    @ApiModelProperty(value = "显示顺序")
    private Integer orderNum;

    @TableField(value = "`tab_close`")
    @ApiModelProperty(value = "离开是否关闭标签页（0否 1是）")
    private Integer tabClose;

    @TableField(value = "`link_type`")
    @ApiModelProperty(value = "链接类型", notes = "0：内部 1：外部")
    private Integer linkType;

    @TableField(value = "`cache_enable`")
    @ApiModelProperty(value = "是否缓存（0缓存 1不缓存）")
    private Integer cacheEnable;

    @TableField(value = "`visible`")
    @ApiModelProperty(value = "显示状态（0显示 1隐藏）")
    private Integer visible;

    @TableField(exist = false)
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<SysMenu> children = new ArrayList<>();

    @Transient
    public String getKey() {
        return this.menuId == null ? IdUtils.fastUUID() : this.menuId.toString();
    }

    public String getTypeName() {
        return this.menuType.label();
    }

    private String convert(Integer num, String str) {
        String[] split = str.split(",");
        return num == 0 ? split[0] : split[1];
    }

    public String getTabCloseName() {
        return convert(this.tabClose, "不关闭,关闭");
    }

    public String getVisibleName() {
        return convert(this.visible, "显示,隐藏");
    }

    public String getLinkTypeName() {
        return convert(this.linkType, "内部链接,外部链接");
    }

    public String getCacheName() {
        return convert(this.cacheEnable, "不缓存,缓存");
    }


}
